package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.example.wanbaqian.R;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.DesUtils;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.MobileSecurePayer;
import com.example.wanbaqian.utils.SPUtils;

import butterknife.BindView;

/**
 * 银行卡
 */
public class FindEdiBankActivity extends BaseActivity implements HttpCallback {
    @BindView(R.id.edi_bankid)
    EditText ediBankid;
    @BindView(R.id.edi_bankname)
    EditText ediBankname;
    @BindView(R.id.ed_IDCard)
    EditText edIDCard;
    private String bank_account;

    private static final String TAG = "FindEdiBankActivity";
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CommonValue.REQUEST_CODE1:
                    Log.i(TAG, "handleMessage: " + msg.obj);
                    Log.i(TAG, "handleMessage: " + msg);
                    if (!TextUtils.isEmpty(msg.obj.toString())) {
                        if (JsonUtils.getInstance().getJsonObject(msg.obj.toString(), "ret_code").equals("0000")) {
                            HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.BANK_CALLBACK, mContext,
                                    new String[]{"accessid", "result_sign", "ret_msg", "no_agree"},
                                    new Object[]{SPUtils.getAccessid(mContext),
                                            JsonUtils.getInstance().getJsonObject(msg.obj.toString(), "result_sign"),
                                            JsonUtils.getInstance().getJsonObject(msg.obj.toString(), "ret_msg"),
                                            JsonUtils.getInstance().getJsonObject(msg.obj.toString(), "no_agree")}),
                                    null, CommonValue.REQUEST_CODE3, FindEdiBankActivity.this);
                        } else {
                            showToast(JsonUtils.getInstance().getJsonObject(msg.obj.toString(), "ret_msg"));
                        }
                    }
                    break;
            }
        }
    };

    @Override
    protected int getLayout() {
        return R.layout.activity_findedibank;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.balck);
        setToolBar("银行卡添加");
        toolBar_tvRight.setText("完成");
        bank_account = getIntent().getStringExtra("bank_account");
        ediBankid.setText(bank_account);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        toolBar_tvRight.setVisibility(View.VISIBLE);

        toolBar_tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addBank();
            }
        });
    }
    /**
     * 添加银行卡
     */
    public void addBank() {

        String account = ediBankid.getText().toString();
        String name = ediBankname.getText().toString();
        String IDCard = edIDCard.getText().toString();
        if (TextUtils.isEmpty(account) || TextUtils.isEmpty(name) || TextUtils.isEmpty(IDCard)) {
            showToast("请输入正确的信息");
        } else {
            HttpUtils.getInstance().post(GetJsonUtils.getInstance()
                            .getRequest(HttpURL.BANK_ADD, mContext,
                                    new String[]{"accessid", "bank_account", "id_name", "id_no", "return_url"}
                                    , new String[]{SPUtils.getAccessid(mContext), DesUtils.encrypt(account, SPUtils.getDesKey(mContext)),
                                            name, DesUtils.encrypt(IDCard, SPUtils.getDesKey(mContext)), ""})
                    , mContext, null, CommonValue.REQUEST_CODE2, this);
        }
    }
    @Override
    protected void initDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.BANK_USERINFO, mContext,
                new String[]{"accessid"}, new Object[]{SPUtils.getAccessid(mContext)}),
                HttpURL.BANK_USERINFO, CommonValue.REQUEST_CODE1, this);
    }
    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        Log.i(TAG, "银行卡信息： "+content.toString());
        switch (requestCode) {
            case CommonValue.REQUEST_CODE1:
                String IDCard = DesUtils.decrypt(JsonUtils.getInstance().getJsonObject(content, "id_no"), SPUtils.getDesKey(mContext));
                String name = JsonUtils.getInstance().getJsonObject(content, "id_name");
                ediBankname.setText(name);
                edIDCard.setText(IDCard);
                if (!TextUtils.isEmpty(IDCard) || !TextUtils.isEmpty(name)) {
                    ediBankname.setKeyListener(null);
                    edIDCard.setKeyListener(null);
                }
                break;
            case CommonValue.REQUEST_CODE2:
                new MobileSecurePayer().payRepaySign(content, handler, CommonValue.REQUEST_CODE1, this, false);
                break;
            case CommonValue.REQUEST_CODE3:
                finish();
                showToast("绑定银行卡成功");
                break;
        }
    }


    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }

    /**
     * 修改服务器返回的订单信息
     *
     * @param content
     * @return
     */
//    private LLPayOrder constructSignCard(String content) {
//        LLPayOrder llPayOrder = JsonUtils.getInstance().jsonToObjectk(content, LLPayOrder.class);
//        llPayOrder.setSign(null);
//        llPayOrder.setId_type(null);
//        String LLContent = LLBaseHelper.sortParamForSignCard(llPayOrder);
//        llPayOrder.setSign(LLRsa.sign(LLContent, CommonValue.LL_RSA_PRIVATE));
//        Log.i(TAG, "constructSignCard: " + LLContent);
//        return llPayOrder;
//    }
}
